<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="getTitle" @ok="handleSubmit">
    <BasicForm @register="registerForm" style="margin-top: 20px">
      <template #cols="{ model, field }">
        <Space>
          <Checkbox v-model:checked="model[field]">列</Checkbox>
          <Checkbox v-model:checked="dataRef.query">工具条按钮及查询</Checkbox>
        </Space>
      </template>
      <template #byModuleCode="{ model, field }">
        <Select
          :options="list"
          v-model:value="model[field]"
          placeholder="请选择"
          :field-names="{
            label: 'name',
            value: 'code',
          }"
          :style="{ width: '100%' }"
        />
      </template>
    </BasicForm>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, computed } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { Space, Checkbox, Select } from 'ant-design-vue';
  import { syncFormSchema } from './formModule.data';
  import { getVisualSyncModule } from '/@/api/module/module';

  export default defineComponent({
    name: 'FormModuleSyncModal',
    components: {
      BasicModal,
      BasicForm,
      Space,
      Checkbox,
      Select,
    },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      const { createMessage } = useMessage();
      const isUpdate = ref(false);
      const dataRef = ref<Recordable>({});
      const list = ref<Recordable[]>([]);
      const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
        setModalProps({ confirmLoading: false, width: '30%', minHeight: 60 });
        resetFields();
        try {
          list.value = data?.list ?? [];
          dataRef.value = {
            ...data.record,
            byModuleCode: list.value && list.value.length ? list.value[0].code : '',
            cols: true,
            query: true,
          };
          isUpdate.value = data?.isUpdate;
          setFieldsValue({ ...dataRef.value });
        } catch (err) {
          console.error(err);
        }
      });

      const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
        labelWidth: 80,
        schemas: syncFormSchema,
        showActionButtonGroup: false,
      });

      const getTitle = computed(() => `同步模块：${dataRef.value.name}`);

      async function handleSubmit() {
        try {
          setModalProps({ confirmLoading: true });

          const values = await validate();
          const formData = Object.assign({}, dataRef.value, values);
          if (formData.byModuleCode == formData.moduleCode) {
            createMessage.warning('不能选择相同的模块');
            return;
          }
          formData.cols = formData.cols ? '1' : '0';
          formData.query = formData.query ? '1' : '0';
          await getVisualSyncModule(formData);
          createMessage.success('操作成功');
          closeModal();
          emit('success');
        } catch (err) {
          console.error(err);
        } finally {
          setModalProps({ confirmLoading: false });
        }
      }

      return {
        registerModal,
        getTitle,
        handleSubmit,
        registerForm,
        list,
        dataRef,
      };
    },
  });
</script>
<style lang="less" scoped>
  :deep(.scrollbar__view) {
    :deep (div) {
      min-height: 80px;
    }
  }
</style>
